//

if (!window.Sigma){
	window.Sigma={};
}
Sigma.Profiler=function(id){
	var Me=this;
	Me.id=id;
	Me.disabled=false;
	Me.startTime=0;
	Me.endTime=0;
	Me.timePointList=[];

	Me.timePart={};
	Me.timePartList=[];

	Me.record=function(infoI){
		if (Me.disabled){
			return;
		}
		var nowTime=Me.now();
		var hasTimePointNum=Me.timePointList.length;
		var lastTime=hasTimePointNum>0?Me.timePointList[hasTimePointNum-1].time: Me.startTime ;
		var timePoint={
			no : hasTimePointNum,
			info : (infoI || "STEP "+(hasTimePointNum-1)+" -- " +hasTimePointNum)+'',
			time :  nowTime,
			elapsed : nowTime - lastTime
		};
		Me.timePointList.push(timePoint);
		return timePoint;
	};

	Me.before=function(infoI){
		if (Me.disabled){
			return;
		}
		var nowTime=Me.now();
		var hasTimePartNum = Me.timePartList.length;
		var timePart={
			no : hasTimePartNum,
			info : (infoI || "PART "+infoI)+'' ,
			beforeTime :  nowTime,
			afterTime  : nowTime,
			elapsed : 0,
			isOver :false
		};
		Me.timePart[timePart.info]=timePart;
		Me.timePartList.push(timePart);
		return timePart;
	};

	Me.after=function(infoI){
		if (Me.disabled){
			return;
		}
		var nowTime=Me.now();
		var timePart=infoI?Me.timePart[ infoI ] :Me.getLastBeforePart();
		if (!timePart){
			return;
		}

		timePart.afterTime=nowTime;
		timePart.elapsed=timePart.afterTime- timePart.beforeTime;
		timePart.isOver=true;
		return timePart;

	};

	Me.getLastBeforePart=function(){
		var lastIndex=Me.timePartList.length-1;
		var last;

		while ( (last=Me.timePartList[lastIndex]) && lastIndex>=0){
			if (!last.isOver){
				return last;
			}
			lastIndex--;
		}
		return null;
	};
	


	Me.output=function(msgZone,msg){
		if (msgZone){

			if (typeof msgZone == 'string'){
				msgZone=Sigma.doc.getElementById(msgZone);
			}
			
			msgZone.innerHTML=msg;
		}else{
			alert(msg);
		}
		var _orgTitle= Sigma.doc.title , _flagIndex=Sigma.doc.title.indexOf('--- Total: ');
		_flagIndex=_flagIndex<0?_orgTitle.length:_flagIndex;
		Sigma.doc.title= _orgTitle.substring(0,_flagIndex)+ ' --- Total: '+(Me.endTime-Me.startTime)+' ms';
	};

	Me.outputPoint=function(msgZone){
		Me.output(msgZone,Me.getPointResult('<br/>\n'));
	};

	Me.outputPart=function(msgZone){
		Me.output(msgZone,Me.getPartResult('<br/>\n'));
	};


	Me.getPointResult=function(sp){
		return Me.getResult(Me.timePointList,sp);
	};

	Me.getPartResult=function(sp){
		return Me.getResult(Me.timePartList,sp);
	};

	Me.getResult=function(list, sp){
		if (Me.disabled){
			return;
		}
		Me.endTime=Me.now();
		var totalTime=Me.endTime-Me.startTime;

		sp = sp || "\n" ;
		var summary = [ "* START\t:\t" +Me.formatDate(Me.startTime) ,
						"* END\t:\t" +Me.formatDate(Me.endTime) ,
						"* TOTAL\t:\t"+ totalTime +sp].join(sp);
		var result= [];
		for (var i=0;i<list.length ;i++ ){
			var tp=list[i];
			result.push(tp.info+"\t:\t"+tp.elapsed);
		}
		return summary+sp+result.join(sp);
	};

	
	Me.R=Me.record;

	Me.start=function(){
		Me.startTime= this.now();
		Me.timePointList=[];
		Me.record("Profiler "+(Me.id||'')+" START");
		return Me;
	};

	Me.startParts=function(){
		Me.startTime= this.now();
		Me.timePart={};
		Me.timePartList=[];
		return Me;
	};

	Me.now=function(){
		return new Date().getTime();
	};


	Me.formatDate=function(date,cD,cT,cDT){
		if (typeof date =='number')	{
			date=new Date(date);
		}
		cD=cD||'-';
		cT=cT||':';
		cDT=cDT||' ';
		var dateD=[
			date.getFullYear(),
			date.getMonth()+1,
			date.getDate()
		];
		var dateT=[
			date.getHours(),
			date.getMinutes()+1,
			date.getSeconds(),
			date.getMilliseconds()
		];
		return dateD.join(cD)+cDT+dateT.join(cT);
	};


	Me.start();
	Me.startParts();

};

//////////////////////////////////////////////////////

